<template>
	<view class="container">
		<view class="boxsssssss" v-if="!isLogin"><text @click="$gourl('/pages/index/login')">登录</text>后查看</view>
		<view class="head" v-if="isLogin">
			<view class="text">购物车({{cartList.length}})</view>
			<view class="text" style="color:#EA3D37 ;" @click="manageClick" v-if="manage == false">管理</view>
			<view class="text" style="color:#EA3D37 ;" @click="manageClick" v-else>完成</view>
		</view>
		<view class="content" v-if="isLogin">
			<view v-for="(item, index) in cartList" :key="item.id"
				style="margin-bottom: 30rpx;border-radius: 20rpx;overflow: hidden;">
				<u-swipe-action :show="item.show" :index="index" @click="click" @open="open" :options="options">
					<view class="list_box">
						<view class="content_con">
							<view class="content_boxs">
								<view class="select" style="margin-top: 46rpx;" @click="danSelect(index)">
									<u-icon name="checkmark-circle-fill" v-if="item.select" size="44" color="#EA3D37">
									</u-icon>
									<view class="noselect" v-else></view>
								</view>
								<view style="flex: 1;width: 580rpx;">
									<view style="flex: 1;width: 580rpx; margin-bottom: 20rpx;">
										<view>
											<view class="content_box">
												<view class="img_box">
													<image :src="item.productInfo.image" mode="aspectFill"></image>
												</view>
												<view class="big_infos">
													<view class="name">
														<view>{{item.productInfo.store_name}}</view>
													</view>
													<view class="sku">
														<view class="txt">
															{{item.productInfo.attrInfo.suk}}
														</view>
														<view class="wd_box" v-if="item.bd_value !== ''"><text
																class="ice"
																style="margin-right: 10rpx;">冰冻{{item.bdInfo.bd}}</text>|<text
																style="margin-left: 10rpx;">常温{{item.bdInfo.cw}}</text>
														</view>
													</view>
													<view class="num">
														<view class="price"><text>￥</text>{{item.productInfo.attrInfo.price}}
															<text class="ot">￥{{item.productInfo.attrInfo.ot_price}}</text>
														</view>
														<view class="number">
															<u-number-box v-model="item.cart_num" bg-color="#EA3D37"
																input-width="40" @change="valChange(item,$event)"
																:min="1">
															</u-number-box>
														</view>
													</view>
												</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</u-swipe-action>
			</view>

		</view>
		<view class="bottom" v-if="isLogin && !manage" @click="bayShop">
			<view class="total">共{{number}}件，合计：<text>￥{{(total).toFixed(2)}}</text></view>
			<view class="btn" @click="goPay">立即购买</view>
		</view>
		<view class="bottom" v-if="isLogin && manage">
			<view class="total">全选</text></view>
			<view class="btn" @click="deletCart">删除</view>
		</view>
		<!-- <view class="login_tips" v-if="!isLogin">
			<view class="tips">登录认证后可查看商品价格</view>
			<view class="tips_bt" @click="$gourl('/pages/index/login')">去登录</view>
		</view> -->
		<!-- <u-loadmore :status="status" v-if="isLogin && status !== 'nomore'" /> -->
		<u-modal v-model="show" :content="content" @confirm="deletCart"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLogin: false,
				value: 1,
				shopArr: [],
				show: false,
				content: '确定删除所选商品吗？',
				selsect: false,
				manage: false,
				cartList: [],
				failure: [],
				total: 0,
				number: 0,
				page: 1,
				pageSize: 10,
				count: 1,
				status: 'loadmore',
				selectList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}]
			}
		},
		onShow() {
			if (uni.getStorageSync('userToken')) {
				this.isLogin = true
				this.getData()
				this.total = 0
				this.number = 0
			} else {
				this.isLogin = false
			}
		},
		methods: {
			open() {},
			delGoods() {},
			click(e) {
				this.$allApi.delCar({
					ids: this.cartList[e].id
				}).then(res => {
					this.$u.toast('删除成功')
					this.getData()
				})
			},
			async getData() {
				await this.$allApi.getCarList().then(res => {
					let datas = res.data.valid
					for (let item of datas) {
						item.bdInfo = {}
						item.bdInfo = JSON.parse(item.bd_value)
						console.log(item.bdInfo);
						if (item.bdInfo.type == '全冰') {
							item.bdInfo.bd = item.cart_num * item.productInfo.attrInfo.num
							item.bdInfo.cw = 0
						} else if (item.bdInfo.type == '一半冰一半不冰') {
							item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num / 2)
							item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
						} else if (item.bdInfo.type == '常温') {
							item.bdInfo.bd = 0
							item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num
						} else if (item.bdInfo.type == '自定义冰冻数量') {
							item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num * item
								.bdInfo.bfNumber)
							item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
						}
						item.select = false
						item.total = 0
						item.total = Number(item.truePrice) * item.cart_num
						for (let item2 of this.selectList) {
							if (item.id == item2) {
								item.select = true
							}
						}
					}
					this.cartList = datas
					this.duoSelect()
					this.$forceUpdate()
				})
			},
			duoSelect() {
				this.selectList = []
				this.number = 0
				this.total = 0
				for (let item of this.cartList) {
					if (item.select) {
						this.total += item.total
						this.number += item.cart_num
						this.selectList.push(item.id)
					}
				}
			},
			danSelect(index) { //单买
				this.cartList[index].select = !this.cartList[index].select
				let yes = this.cartList.filter(item => {
					return item.select == true
				})
				if (this.cartList[index].select == true) {
					this.selectList.push(this.cartList[index].id)
					this.number += this.cartList[index].cart_num
					this.total = this.total + this.cartList[index].total
					if (yes.length == this.cartList.length) {
						this.selsect = true
						this.cartList[index].select = true
					}
				} else {
					this.total = this.total - this.cartList[index].total
					for (let index2 in this.selectList) {
						console.log(index2);
						if (this.selectList[index2] == this.cartList[index].id) {
							this.selectList.splice(index2, 1)
						}
					}
					if (yes.length < this.cartList.length) {
						this.selsect = false
						this.cartList[index].select = false
						this.number -= this.cartList[index].cart_num
					}
				}
				if (this.total < 0) {
					this.total = 0
				}

				console.log(this.selectList);
				this.$forceUpdate()
			},
			valChange(item, e) { //数量增加
				if (item.bdInfo.type == '全冰') {
					item.bdInfo.bd = item.cart_num * item.productInfo.attrInfo.num
					item.bdInfo.cw = 0
				} else if (item.bdInfo.type == '一半冰一半不冰') {
					item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num / 2)
					item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
				} else if (item.bdInfo.type == '常温') {
					item.bdInfo.bd = 0
					item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num
				} else if (item.bdInfo.type == '自定义冰冻数量') {
					item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num * item
						.bdInfo.bfNumber)
					item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
				}
				this.$allApi.carNmuUpdate({
					id: item.id,
					number: e.value,
					el_value: JSON.stringify(item.bdInfo)
				}).then(res => {
					this.total = 0
					this.number = 0
					this.getData()
				})
			},
			manageClick() { //点击管理
				this.manage = !this.manage
				let select = 0
				let length = 0
				for (let item of this.cartList) {
					if (item.select == true) {
						select++
					}
				}
				if (select == this.cartList.length) {
					this.selsect = true
				} else {
					this.selsect = false
				}
			},
			allselect() {
				this.selsect = !this.selsect
				let money = 0
				if (this.selsect == true) {
					for (let item of this.cartList) {
						item.select = true
						item.total = item.price * item.num
						money += item.total
					}
					this.total = money
					this.number = this.cartList.length
				} else {
					for (let item of this.cartList) {
						item.select = false
						item.total = item.price * item.num
						money += item.total
					}
					this.total = this.total - money
					this.number = 0
					if (this.total < 0) {
						this.total = 0
					}
				}
			},
			delectClick() { //点击删除
				this.show = true
			},
			goPay() {
				let orderPayList = []
				for (let item of this.cartList) {
					if (item.select == true) {
						orderPayList.push(item.id)
					}
				}
				if (orderPayList.length == 0) {
					this.$u.toast('请选择您要结算的商品')
					return
				} else {
					this.$gourl('/mallPage/confirmOrder?car_id=' + orderPayList.join(','))
				}
				// if()
			},
			deletCart() {
				let cart = []
				for (let item of this.cartList) {
					if (item.select == true) {
						cart.push(item.id)
					}
				}
				this.$allApi.delCar({
					ids: cart.join(',')
				}).then(res => {
					this.$u.toast('删除成功')
					this.total = 0
					this.select = false
					this.number = 0
					this.getData()
				})
			},
			bayShop() {
				let shops = []
				let shop_num = 0
				let dan = {
					group_id: '',
					goods_list: []
				}
				let tuan = {}
				for (let item of this.cartList) {
					for (let item2 of item.group) {
						for (let item3 of item2.cart) {
							if (item2.is_single == 1) {
								if (item3.select == true) {
									dan.group_id = item2.id
									dan.invitation_id = item2.cart[0].invitation_id
									dan.goods_list.push({
										goods_id: item3.goods_id,
										sku_price_id: item3.sku_price_id,
										goods_num: item3.goods_num,
									})
									shops.push(dan)
									shop_num++
								}
							} else {
								console.log('item2', item2)
								if (item2.select == true) {
									tuan.invitation_id = item2.cart[0].invitation_id
									tuan.group_id = item2.id
									tuan.num = item3.goods_num
									shops.push(tuan)
									shop_num++
								}
							}
						}
					}
				}
				if (shops.length == 0) {
					this.$u.toast('请选择您要结算的商品')
					return
				}
				let aaa = this.quchong(shops)
				let datas = {
					shop: aaa,
					type: 'cart',
					shop_num: shop_num
				}
				this.$u.api.getShopPrice({
					group_list: datas.shop,
					from: this.type,
					address_id: 0,
				}).then(res => {
					getApp().shoppingInfo = JSON.stringify(datas)
					// uni.setStorageSync('shoppingInfo', JSON.stringify(datas))
					this.$gourl('/order/shopping/group')
					uni.hideLoading()
				})

			},
			quchong(arr) {
				let res = {}
				let map = new Map();
				for (let item of arr) {
					if (!map.has(item.group_id)) {
						map.set(item.group_id, item);
					}
				}
				return [...map.values()];
				return Object.values(res)
			}
		}
	}
</script>
<style>
	.u-number-input {
		background: #fff !important;
	}

	.u-icon-minus,
	.u-icon-plus {
		color: #fff !important;
		width: 40rpx !important;
		height: 40rpx !important;
	}
</style>

<style scoped lang="scss">
	.boxsssssss {
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #666;

		text {
			color: #EA3D37;
		}
	}

	.login_tips {
		width: 750rpx;
		height: 80rpx;
		background: rgba(0, 0, 0, .5);
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 24rpx;
		color: #fff;
		padding: 0 30rpx;
		z-index: 999;
		bottom: 0;
		left: 0;

		.tips_bt {
			width: 134rpx;
			height: 48rpx;
			border-radius: 394rpx;
			background: rgba(64, 174, 54, 1);
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.types {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.noselect {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: #ccc 1px solid;
	}

	.container {
		min-height: 100vh;
		background: #f6f6f6;
		position: relative;
		padding-bottom: 100rpx;
	}

	.head {
		position: sticky;
		top: 0;
		left: 0;
		width: 100vw;
		z-index: 9999;
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 90rpx;
		background: #fff;
		font-size: 28rpx;
		padding: 0 20rpx;
	}

	.content {
		padding: 20rpx;
		padding-bottom: 20rpx;

		.select {
			width: 70rpx;
		}

		.list_box {
			padding: 20rpx;
			background: #FFFFFF;
			border-radius: 10rpx;
		}

		.content_con {


			.head {
				padding: 20rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #eee;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						border-radius: 10rpx;
						margin-right: 20rpx;
					}
				}
			}

			.content_boxs {
				margin-top: 20rpx;
				display: flex;
				// align-items: center;
				justify-content: space-between;
			}

			.content_box {
				flex: 1;
				display: flex;
				justify-content: space-between;

				.img_box {
					width: 140rpx;
					height: 140rpx;

					image {
						width: 140rpx;
						height: 140rpx;
						border-radius: 20rpx;
					}
				}

				.big_infos {
					flex: 1;
					padding-left: 20rpx;

					.name {
						font-size: 32rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
					}

					.sku {
						font-size: 24rpx;
						color: #999;
						display: flex;
						line-height: 2;
						justify-content: flex-start;
						align-items: center;

						.wd_box {
							margin-left: 20rpx;
							display: flex;
							margin-left: 20rpx;
							align-items: center;
							justify-content: space-between;
							background: #F6F6F7;
							height: 32rpx;
							border-radius: 6rpx;
							font-size: 20rpx;
							padding: 0 14rpx;

							.ice {
								color: #26ACF5;
							}
						}
					}

					.num {
						display: flex;
						justify-content: space-between;
						align-items: center;
						font-size: 40rpx;

						text {
							font-size: 24rpx;
						}

						.ot {
							color: #999;
							margin-left: 10rpx;
							text-decoration: line-through;
						}

						.price {
							color: #EA3D37;
						}
					}
				}
			}

			.small_info {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					width: 95rpx;
					height: 95rpx;
					border-radius: 10rpx;
				}

				.infos {
					flex: 1;
					padding-left: 20rpx;
					width: 596rpx;
					line-height: 1.8;

					.name {
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.miaosu {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		height: 100rpx;
		background: #ffff;
		box-shadow: 0px 2px 5px 2px #eee;
		z-index: 99;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.total {
			text {
				color: #EA3D37;
				font-weight: bold;
			}

			padding-left: 30rpx;
		}

		.btn {
			height: 100%;
			width: 220rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background: #EA3D37;
		}
	}
</style>